package br.com.ufpr.sgp.components;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;

import br.com.ufpr.util.base.BasePage;

public class MenuItem extends AbstractReadOnlyModel<MenuItem> {
    private static final long serialVersionUID = -7232905392132962545L;

    private final IModel<String> lnkLabel;

    private final Class<? extends BasePage> clazz;

    private boolean divider;
    private boolean header;

    private final List<IModel<MenuItem>> submenus = new ArrayList<IModel<MenuItem>>();

    public MenuItem(IModel<String> lnkLabel) {
        super();
        this.lnkLabel = lnkLabel;
        this.clazz = null;
    }

    public MenuItem(IModel<String> lnkLabel, Class<? extends BasePage> clazz) {
        super();
        this.lnkLabel = lnkLabel;
        this.clazz = clazz;
    }

    public IModel<String> getLnkLabel() {
        return lnkLabel;
    }

    public Class<? extends BasePage> getClazz() {
        return clazz;
    }

    public void addSubmenu(final MenuItem submenu) {
        this.submenus.add(submenu);
    }

    public boolean isUmDosSubMenusAtivos(final Class<? extends BasePage> classPagAtiva) {
        if (getClazz() != null && getClazz().isAssignableFrom(classPagAtiva)) {
            return true;
        }
        for (IModel<MenuItem> sub : getSubmenus()) {
            if (sub.getObject().isUmDosSubMenusAtivos(classPagAtiva)) {
                return true;
            }
        }
        return false;
    }

    public List<IModel<MenuItem>> getSubmenus() {
        return submenus;
    }

    @Override
    public MenuItem getObject() {
        return this;
    }

    public boolean isDivider() {
        return divider;
    }

    public void setDivider(boolean divider) {
        this.divider = divider;
    }

    public boolean isHeader() {
        return header;
    }

    public void setHeader(boolean header) {
        this.header = header;
    }

}
